package com.dykj.apps.web.sysmanager.service;

import java.util.ArrayList;
import java.util.List;

import com.dykj.apps.web.sysmanager.dto.SysRoleDTO;
import com.dykj.platform.framework.common.paging.Page;
import com.dykj.platform.framework.exception.ApplicationException;
import com.dykj.platform.framework.exception.DatabaseException;
import com.dykj.platform.framework.utils.BeanUtils;
import com.dykj.apps.web.sysmanager.dao.IRoleManagerDAO;
import com.dykj.apps.web.sysmanager.dao.IUserManagerDAO;
import com.dykj.apps.web.sysmanager.dao.po.SysRole;
import com.dykj.apps.web.sysmanager.dao.po.SysUser;

public class RoleManagerBSImpl implements IRoleManagerBS {
	private IRoleManagerDAO dao;
	private IUserManagerDAO userdao;

	/**
	 * 删除一个角色
	 * (non-Javadoc)
	 * @see com.dykj.apps.web.sysmanager.service.IRoleManagerBS#delUser(java.lang.String)
	 **/
	public int delRole(Long key,String groupid) throws ApplicationException {
		try {
			List user = userdao.queryHaveRoleUser(key, groupid);
			if(user.size()>0){
				return -1;
			}else{
				dao.delByKey(SysRole.class, key);
			}
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return 0;
	}

	/**
	 * 取得一个角色
	 */
	public SysRoleDTO getOneSysRole(Long id) throws ApplicationException {
		SysRoleDTO dto = new SysRoleDTO();
		try {
			Object obj = dao.getObject(SysRole.class, id);
			if(obj!=null){
				SysRole role = (SysRole)obj;
				BeanUtils.copyProperties(dto, role);
			}
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return dto;
	}

	/**
	 * 翻页查询
	 */
	public List querySysRole(SysRoleDTO dto, Page page) throws ApplicationException {
		List<SysRoleDTO> list = new ArrayList <SysRoleDTO>();
		try {
			List roleList = dao.query(dto, page);
			for(int i=0;i<roleList.size();i++)
			{
				SysRoleDTO roledto = new SysRoleDTO();
				SysRole role = (SysRole) roleList.get(i);
				BeanUtils.copyProperties(roledto , role);
				list.add(roledto);
			}
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return list;
	}

	/**
	 * 保存修改角色
	 * 
	 */
	public int saveOrUpdateRole(SysRoleDTO dto,String[] userList) throws ApplicationException {

		try {
			SysRole sole = new SysRole();
			BeanUtils.copyProperties(sole, dto);
			//dto里roleid ""值用BeanUtils.copyProperties()拷贝到sole里就变0了,所以加了一个 ==0条件
			if(sole.getRoleid()==null || "".equals(sole.getRoleid())||sole.getRoleid() == 0 ){
				sole.setRoleid(Long.parseLong(dao.getKeyIndex("roleid", "sysrole")));
				dto.setRoleid(sole.getRoleid().toString());
			}
			dao.saveOrUpdate(sole);
			userdao.updateUserRole(Long.valueOf(dto.getRoleid()));
			for(int i=0;i<userList.length;i++){
				if(!"".equals(userList[i])){
					SysUser user = (SysUser)userdao.getObject(SysUser.class, userList[i]);
					user.setRoleid(Long.valueOf(dto.getRoleid()));
					userdao.update(user);
				}
			}
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return 0;
	}
	
	public List queryBydesc(String roledesc,String roleid, String creator) throws ApplicationException {
		List list = new ArrayList();
		try {
			list =  dao.queryBydesc(roledesc,roleid,creator);
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return list;
	}
	
	public IRoleManagerDAO getDao() {
		return dao;
	}

	public void setDao(IRoleManagerDAO dao) {
		this.dao = dao;
	}

	public IUserManagerDAO getUserdao() {
		return userdao;
	}

	public void setUserdao(IUserManagerDAO userdao) {
		this.userdao = userdao;
	}


}
